#!/usr/local/bin/ruby
=begin

Copyright 2010 Arachne Jericho <arachne.jericho@gmail.com>

This file is part of RubyTea.

RubyTea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

RubyTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RubyTea.  If not, see <http://www.gnu.org/licenses/>.

=end
# 
# CGI form to allow easily creating WP-orientated "boxes" for 
# tea images, etc. 
#

require 'ruby-tea.rb'
require 'cgi'

header('Box a Tea', 10)

cgi = CGI.new
blender = cgi['blender']
tea = cgi['tea']

def output_blender_form
    puts <<-END
    Pick a blender: 
    <form method="PUT">
      <select name="blender">
        <option value="" selected>Select a blender...</option>
    END
    puts (($tea_collection.keys.sort.map { |b| %Q(<option value="#{CGI::escapeHTML b}">#{CGI::escapeHTML b}</option>)}).join("\n"))
    puts <<-END
      </select>
      <input type="submit" />
    </form>
    END
end

def output_tea_form(blender)
    puts <<-END
    Blender '#{CGI::escapeHTML blender}' picked <br />
    <form method="PUT">
      <input type="hidden" name="blender" value="#{CGI::escapeHTML blender}" />
      <select name="tea">
        <option value="" selected>Select a tea...</option>
    END

    puts (($tea_collection[blender].keys.sort.map { |t| %Q(<option value="#{CGI::escapeHTML t}">#{CGI::escapeHTML t}</option>)}).join("\n"))

    puts <<-END
      </select>
      <input type="submit" />
    </form>
    END
end

def output_result(tea_url, tea_caption, img_src)

    tc = tea_url ? "<a href='#{tea_url}'>#{tea_caption}</a>" : tea_caption
    img = %Q(#{tea_url ? %Q(<a href="#{tea_url}">) : ''}<img alt="#{tea_caption}" title="#{tea_caption}" src="#{img_src}" />#{tea_url ? '</a>' : ''})

    puts <<-END 
    <img src="#{img_src}" /><br />
    <textarea cols="80" rows="10" onClick="select()">
    END
    puts (CGI::escapeHTML(<<-END
[caption id="" align="alignleft" width="100" caption="#{tc}"]#{img}[/caption]
    END
    ))
    puts '</textarea><br /><br />'
end

if blender.empty? 
    # Output form to pick blender
    output_blender_form
elsif tea.empty?
    if !$tea_collection.has_key? blender
        puts <<-END
        <strong>ERROR:</strong> Blender "#{CGI::escapeHTML blender}" does not exist in the collection!<br />
        END
        output_blender_form
    else
        # Output form to pick tea from blender
        output_tea_form blender
    end
else
    if !$tea_collection.has_key? blender
        puts <<-END
        <strong>ERROR:</strong> Blender '#{CGI::escapeHTML blender}' does not exist in the collection!<br />
        END
        output_blender_form
    elsif !$tea_collection[blender].has_key? tea
        puts <<-END
        <strong>ERROR:</strong> Blender "#{CGI::escapeHTML blender}" does not have tea "#{CGI::escapeHTML tea}" in the collection!<br />
        END
        output_tea_form blender
    else
        # Output result
        tea_info = $tea_collection[blender][tea]
        tea_url = tea_info.has_key?('URL') ? CGI::escapeHTML(tea_info['URL']) : nil
        tea_caption = CGI::escapeHTML "#{blender}: #{tea}"

        puts 'Copy-pasta your choice:<br /><br />'

        img_src = CGI::escapeHTML "#{RUBY_TEA_BASE_IMG_URL}/#{anchorize blender}/#{anchorize tea}.jpg"
        output_result(tea_url, tea_caption, img_src)

        tea_type = $tea_collection[blender][tea]['Type'].capitalize
        img_src = CGI::escapeHTML "#{RUBY_TEA_BASE_IMG_URL}/Anonymous/#{tea_type}_Tea.png"
        output_result(tea_url, tea_caption, img_src)

        output_blender_form
    end
end

footer()
